package com.duotai.duotai4;

import java.util.ArrayList;

public class PeopleDao implements BaseDao{
    //创建一个集合之后就可以存储学生和老师两种对象了
    ArrayList<People> p = new ArrayList<People>();

    @Override
    public void add(People people) {
        p.add(people);
    }

    @Override
    public void delete(int id) {
        for(int i = 0; i < p.size(); i++){
            if(p.get(i).getId() == id){
                p.remove(i);
            }
        }
    }

    public void findAll(){
        System.out.println(p);
    }

    public void findByJob(People people){
         //只有Teacher这个类中才有job属性,Student中的属性是major
         //对测试方法中传过来的对象进行了判断
         boolean flag = people instanceof Teacher;
        //我们应该传入的是一个Teacher对象,判断完成之后,符合情况就进行强制类型转换
        Teacher teacher = (Teacher) people;
        String job = teacher.getJob();
         if(flag){
             for(int i = 0; i < p.size(); i++){
                 //对遍历出来的集合中的对象进行遍历,如果是Teacher类型我们再进行强制的数据类型转换
                 if(p.get(i) instanceof Teacher){
                     //遍历集合并且找到集合中的对象取出对象的属性
                     People pe = p.get(i);
                     Teacher t = (Teacher)pe;
                     String job1 = t.getJob();
                     if(job1.equals(job)){
                         System.out.println(p.get(i));
                     }
                 }
             }
         }else{
             System.out.println("类型不对");
         }
    }
}
